/**
 * 
 */
package com.google.code.rgbgoesbetween.gui;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.code.rgbgoesbetween.R;
import com.google.code.rgbgoesbetween.data.AccountStatus;
import com.google.code.rgbgoesbetween.services.DataProvider;
import com.google.code.rgbgoesbetween.services.SessionManager;

/**
 * 
 * Registers a new user to the activity
 * 
 * @author Patrick
 *
 */
public class RegisterActivity extends Activity implements OnClickListener {

	private EditText usernameView;
	private EditText passwordView;
	private EditText confirmView;
	private Button 	 registerButton;
	private TextView notificationsView;
	
	private DataProvider dataProvider = DataProvider.getDataProvider();
	
	/**
	 * Initializes the GUI, session manager, and data provider if necessary and sets up the button listeners.
	 * 
	 */
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		
		// Let data provider inject its dependencies
		dataProvider.init(this);
        
     // Set Hashing Scheme to SHA1
        SessionManager.getSessionManager().init();
        
        notificationsView 	= (TextView) findViewById(R.id.create_user_notifications_field);
        usernameView   		= (EditText) findViewById(R.id.create_user_username_field);
        passwordView   		= (EditText) findViewById(R.id.create_user_password_field);
        confirmView    		= (EditText) findViewById(R.id.create_user_password_confirm_field);
        registerButton 		= (Button)   findViewById(R.id.create_user_register_button);
        
        registerButton.setOnClickListener(this);
	}

	/**
	 * Inflate the options menu; this adds items to the action bar if it is present.
	 */
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_home, menu);
		return true;
	}
	/**
	 * Handles click events from the UI. Attempts to create user through the SessionManager,
	 *  but handles input validation so the session manager doesn't have to.
	 */
	@Override
	public void onClick(View v) {
		if(v == null) {return;}
		if (v.getId() == R.id.create_user_register_button) {
			if (!passwordView.getText().toString().equals(confirmView.getText().toString())) {
				notificationsView.setText("Your passwords do not match. Please try again.");
				return;
			}
			String username = usernameView.getText().toString();
			String password = passwordView.getText().toString();
			AccountStatus status = SessionManager.getSessionManager().createProfile(
					username, 
					password);
			Log.e("User registration", String.format("username: %s password: %s status: %s", 
					username, password, status.toString()));
			if (AccountStatus.STANDARD_MEMBER.equals(status) || 
					AccountStatus.ADMIN_MEMBER.equals(status)) {
				notificationsView.setText("");
				Toast.makeText(this, "User created successfully.", Toast.LENGTH_LONG).show();
				this.finish();
			}
			else {
				notificationsView.setText("That username has been taken. Please select another.");
			}
		}
	}

}
